package com.ksr.logistics.service.impl;

import com.github.pagehelper.PageHelper;
import com.ksr.logistics.mapper.OrderDetailMapper;
import com.ksr.logistics.mapper.OrderMapper;
import com.ksr.logistics.pojo.Order;
import com.ksr.logistics.pojo.OrderDetail;
import com.ksr.logistics.pojo.OrderDetailExample;
import com.ksr.logistics.pojo.OrderExample;
import com.ksr.logistics.service.OrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Override
    public List<Order> selPageOrders(@RequestParam(defaultValue = "1") Integer page,@RequestParam(defaultValue = "10")Integer pageSize, String keyWord) {
        OrderExample example = new OrderExample();
        if (StringUtils.isNotBlank(keyWord)){
            OrderExample.Criteria criteria = example.createCriteria();
            criteria.andShippingNameLike("%"+keyWord+"%");
            OrderExample.Criteria c2 = example.createCriteria();
            c2.andTakeNameLike("%"+keyWord+"%");
            example.or(c2);
        }
        example.setOrderByClause("order_id desc");
        PageHelper.startPage(page,pageSize);
        List<Order> orders = orderMapper.selectByExample(example);
        return orders;
    }

    @Override
    public List<OrderDetail> selOrderDetailByOrderId(Long orderId) {
        OrderDetailExample example = new OrderDetailExample();
        OrderDetailExample.Criteria criteria = example.createCriteria();
        criteria.andOrderIdEqualTo(orderId);
        return orderDetailMapper.selectByExample(example);
    }

    @Override
    public OrderDetail selOrderDetailByDetailId(Long id) {
        return orderDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public int deleteById(Long id) {
        orderDetailMapper.deleteByOrderId(id);
        return orderMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int deleteByIds(List<Long> ids) {
        int row=0;
        for (Long id:ids) {
            row+=deleteById(id);
        }
        return row;
    }

    @Override
    public Order selById(Long id) {
        return orderMapper.selectByPrimaryKey(id);
    }

    @Override
    public int insertOrder(Order order) {
        int row=orderMapper.insert(order);
        if (order.getOrderDetails()!=null){
            for (OrderDetail detail :order.getOrderDetails()){
                detail.setOrderId(order.getOrderId());
                orderDetailMapper.insert(detail);
            }
        }
        return row;
    }

    @Override
    public int updateOrder(Order order) {
        int row=orderMapper.updateByPrimaryKeySelective(order);
        Long orderId=order.getOrderId();
        orderDetailMapper.deleteByOrderId(orderId);
        if (order.getOrderDetails()!=null){
            for (OrderDetail detail :order.getOrderDetails()){
                if (detail.getOrderDetailId()==null){
                    detail.setOrderId(orderId);
                }
                orderDetailMapper.insert(detail);
            }
        }
        return row;
    }

    @Override
    public int deleteDetailById(Long id) {
        return orderDetailMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insertOrderDetail(OrderDetail orderDetail) {
        return orderDetailMapper.insertSelective(orderDetail);
    }

    @Override
    public int updateOrderDetail(OrderDetail orderDetail) {
        return orderDetailMapper.updateByPrimaryKeySelective(orderDetail);
    }
}
